﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DataInfo;
using KenLib.BusinessManager;
using System.Text.RegularExpressions;

public partial class Org_frmO_EmpWorkSkillInput : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((Org_MasterPage)this.Master).changeTitle(" 業務スキル情報登録");
        ((Org_MasterPage)this.Master).HideLiArea("frmO_EmpWorkSkillInput    ");

        //Session["社員ID"] = "10";
        if (Session["社員ID"].ToString() == "")
            Response.Redirect("frmO_EmpDiplomaInput.aspx");

        if (!Page.IsPostBack)
        {
            LoadData();
            ddl_業務スキル.Focus();
        }
        RequiredFieldValidator1.ErrorMessage =  GetError("ER00001");
        //validate_txt_発行日.ErrorMessage =  GetError("ER00001");
        CustomValidator1.ErrorMessage =  GetError("ER00001");
        RequiredFieldValidator3.ErrorMessage =  GetError("ER00007");
        RegularExpressionValidator1.ErrorMessage =  GetError("ER00002");
    }
    
    /// <summary>
    /// Load thong tin len ComboBox
    /// </summary>
    private void LoadData()
    {
        
        ddl_業務スキル.DataSource = new 業務スキルコードManager().SelectAll();
        ddl_業務スキル.DataTextField = "勤務スキル名";
        ddl_業務スキル.DataValueField = "ID";
        ddl_業務スキル.DataBind();
        ddl_業務スキル.Items.Insert(0, new ListItem("--------選択-------", "-11"));


    }
   
    protected void btn_戻る_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmO_EmpDiplomaInput.aspx");
    }
    
    protected void btn_次_Click(object sender, EventArgs e)
    {
        Response.Redirect("frmO_EmpRewPunInput.aspx");
    }
   
    protected void btnリセット_Click(object sender, EventArgs e)
    {
        LoadData();
        ClearTextPage();
        ddl_業務スキル.Focus();
    }

    /// <summary>
    /// Xoa sach text tren page
    /// </summary>
    private void ClearTextPage()
    {
        txt_レベル.Text = "";
        txt_発行機関.Text = "";
        txt_発行日.Text = "";
        //ListItem ltTemp = new ListItem();
        //ltTemp.Text = "-選択-";
        //ltTemp.Value = "-11";
        //int index = ddl_業務スキル.Items.IndexOf(ltTemp);
        ddl_業務スキル.SelectedIndex = 0;

    }

    protected void btn登録_Click(object sender, EventArgs e)
    {

        try
        {
            業務スキル情報 obj = new 業務スキル情報();
            obj.廃止区分 = 0;
            obj.業務スキルコード = Int32.Parse(ddl_業務スキル.SelectedValue.ToString());
            obj.業務スキル名 = ddl_業務スキル.SelectedItem.Text;
            obj.レベル = ConvertHTMLtoText(txt_レベル.Text.Trim());
            obj.発行機関 = ConvertHTMLtoText(txt_発行機関.Text.Trim());

            if (txt_発行日.Text != "")
            {
                DateTime d1 = DateTime.Parse(txt_発行日.Text);
                obj.発行日 = d1;
            }
            obj.社員ID = Int32.Parse(Session["社員ID"].ToString());

            int kq = new 業務スキル情報Manager().InsertPartial(obj);
            if (kq != 0 && kq != -1 && Page.IsValid == true)
            {
                //ClearTextPage();
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "clearText();", true);
                 Response.Redirect("frmO_EmpRewPunInput.aspx", false);
            }

        }
        catch (Exception ex) { }

    }

    /// <summary>
    /// Chuyen chuoi sang ma HTML
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertTexttoHTML(string temp)
    {
        temp = temp.Replace("<", "&lt;");
        temp = temp.Replace(">", "&gt;");
        return temp;
    }
    
    /// <summary>
    /// Chuyen ma HTML sang chuoi
    /// </summary>
    /// <param name="temp"></param>
    /// <returns></returns>
    public string ConvertHTMLtoText(string temp)
    {
        temp = temp.Replace("&lt;", "<");
        temp = temp.Replace("&gt;", ">");
        temp = temp.Replace(Convert.ToString((char)13), "<br/>");
        return temp;
    }

    /// <summary>
    /// Xuat alert ma loi
    /// </summary>
    private void ErrorValid()
    {
        String str;
        str = "<script type='text/javascript'>";
        str += "alert('" + GetError("ER00002") + "');";
        str += "</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "err", str, false);
    }

    /// <summary>
    /// Xuat alert ma loi
    /// </summary>
    private void ErrorRequest()
    {
        String str;
        str = "<script type='text/javascript'>";
        str += "alert('" + GetError("ER00001") + "');";
        str += "</script>";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "err", str, false);
    }

    /// <summary>
    /// Lay thong diep loi ung voi ma loi
    /// </summary>
    /// <param name="errorid"></param>
    /// <returns></returns>
    public string GetError(string errorid)
    {
        Error er = new Error();
        er.ErrorID = errorid;
        ErrorManager frm_error = new ErrorManager();
        DataTable dt = frm_error.getError(er);
        return dt.Rows[0][2].ToString();
    }

    /// <summary>
    /// Kiem tra ngay thang co duoc dinh dung dung ko
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    protected Boolean checkDateValid(String input)
    {
        String pattern = @"^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/.-])(?'month'0?[1-9]|1[012])\2(?'day'((?<!(\2((0?[2469])|11)\2))31)|(?<!\2(0?2)\2)(29|30)|((?<=((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\2\3\2)29)|((0?[1-9])|(1\d)|(2[0-8])))(?:(?=\x20\d)\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}))?)$";
        return Regex.IsMatch(input, pattern);
    }


}
